package data.selection;

import java.awt.Color;

import data.Player;
import data.Position;
import engine.GameEngine;

/**
 * Selection highlight to be used with the Block action
 * @author Julien
 *
 */
public class BlockSelectionObject extends SelectionObject {
	Player player;
	// We need a "hardcopy" of the initial position so that when we remove
	// the selection object, it works correctly
	Position playerPos;
	
	/**
	 * Creates a block selection highlight, surrounding the player and showing
	 * which players can be blocked (and showing ST hints)
	 * @param player the player that will perform the block
	 */
	public BlockSelectionObject( Player player )
	{
		super();
		this.player = player;
		playerPos = new Position(player.x(), player.y());
	}
	
	/**
	 * Checks if the given position is affected by this selection object
	 * @return true if it is affected, false otherwise
	 */
	public boolean isSelected( Position pos )
	{
		return ( color( pos ) != null );
	}
	
	/**
	 * Returns the color of the given position, and null if it is not affected
	 * @return Color the color of the highlight
	 */
	public Color color( Position pos )
	{
		if( Math.abs(pos.x() - playerPos.x()) <= 1 && 
		    Math.abs(pos.y() - playerPos.y()) <= 1   )
		{
			// Exception: current position
			if( pos.x() == playerPos.x() && pos.y() == playerPos.y() )
				return null;
			
			Player target = GameEngine.getInstance().getPlayer( pos );
			
			if( target == null                                                 ||
				GameEngine.getInstance().currentTeam().players().contains(player) == 
				GameEngine.getInstance().currentTeam().players().contains(target)   )
			{
				return m_defaultColor;
			}
			else
			{
				// Compare strengths
				if( player.ST() > 2 * target.ST() )
					return new Color( 0.0f, 1.0f, 0.0f, 0.25f );
				else if( player.ST() > target.ST() )
					return new Color( 0.0f, 0.5f, 0.0f, 0.25f );
				else if( player.ST() == target.ST() )
					return new Color( 0.5f, 0.5f, 0.0f, 0.25f );
				else if( 2 * player.ST() < target.ST() )
					return new Color( 1.0f, 0.0f, 0.0f, 0.25f );
				else // player.ST() < target.ST()
					return new Color( 1.0f, 0.5f, 0.0f, 0.25f );
			}
		}
		else
		{
			return null;
		}
	}
}
